package com.hk.asyncprocess.compensation;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class MemberService {

    //发送欢迎消息的状态
    private Map<Long,Boolean> welcomStatus = new ConcurrentHashMap<>();

    @RabbitListener(queues = RabbitConfiguration.QUEUE)
    public void listen(User user) {
        log.info("receive mq user {}", user.getId());
        welcom(user);
    }

    //发送欢迎消息
    public void welcom(User user) {
        //去重操作
        if (welcomStatus.putIfAbsent(user.getId(), true) == null) {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
            }
            log.info("MemberService: welcome new user {}", user.getId());
        }


    }


}
